#include <stdlib.h>
#include <gjs/gjs.h>
#include "config.h"

void octopus_extern_create_js_context (char* ui_name) {
	const gchar*    js_dir[2];
	GjsContext*     gjs_ctx;
	GError*         error = NULL;
	int             status;

	js_dir[0] = g_build_path (G_DIR_SEPARATOR_S, DATAPATH, ui_name, "js", NULL);
	js_dir[1] = NULL;
	g_debug ("js_dir: %s", js_dir[0]);

	//create JS context
	g_irepository_prepend_search_path (LIBPATH);
	gjs_ctx = gjs_context_new_with_search_path ((gchar**)js_dir);
	if (!gjs_context_eval (gjs_ctx,                                     //context
	                       "const Main = imports.main; Main.start();",  //script
	                       -1,                                          //len
	                       "<main>",                                    //filename
	                       &status,
	                       &error))
	{
		g_error ("error evaling main.js: %s", error->message);
		g_error_free (error);
		exit (EXIT_FAILURE);
	}
}
